package com.xingzhi.edu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingzhi.commonutils.R;
import com.xingzhi.edu.entity.Teacher;
import com.xingzhi.edu.service.TeacherService;
import com.xingzhi.edu.vo.TeacherQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Zero
 * @since 2021-03-22
 */
@RestController
@Api(value = "讲师管理模块")
//Controller表示交给spring管理    @ResponseBody表示返回数据Json
@RequestMapping("/edu/teacher")
public class TeacherController {
    //controller调用service注入service
    @Autowired
    private TeacherService teacherService;
    //访问地址 http://localhost:8001/edu/teacher/findAll
     /**
     *@Description:查询讲师表所有的数据__rest风格
     */
     @ApiOperation(value = "讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        //调用service的方法实现查询所有的操作  因为service也继承了mapper方法
//        List<Teacher> teacherList = teacherService.list(null);
//        return teacherList;
         List<Teacher> teacherList = teacherService.list(null);
         return R.ok().data("items",teacherList);
    }


    /**
    *@Description:根据id,逻辑删除讲师的方法
    */
    //delete请求不能直接用浏览器请求
    @DeleteMapping("{id}")    //id需要通过路径进行传播
    @ApiOperation(value = "根据Id逻辑删除讲师")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true)
                                     @PathVariable Long id){
        //@PathVariable得到路径中的Id值
//        boolean flag = teacherService.removeById(id);
//        return flag;
        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
    *@Description:分页查询讲师
    */
    @GetMapping("pageListTeacher/{current}/{limit}")
    //根据路径传值
    @ApiOperation(value = "分页查询讲师")
     public R pageListTeacher(@ApiParam(name = "current",value = "当前页",required = true)
                              @PathVariable long current,
                              @ApiParam(name = "limit",value = "记录数",required = true)
                              @PathVariable long limit){
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current,limit);
        //调用方法实现分页
        // 调用方法时候  底层封装  把分页所有数据封装到PageTeacher对象里面
        teacherService.page(pageTeacher,null);

        long total = pageTeacher.getTotal();  //得到总记录数
        List<Teacher> pageTeacherRecords = pageTeacher.getRecords();  //数据list集合

        return R.ok().data("total",total).data("rows",pageTeacherRecords);
        //或者
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",pageTeacherRecords);
//        return R.ok().data(map);
//        return R.ok().data("total",total).data()
    }
    /**
    *@Description:根据多条件(名称_头衔_开始时间_结束时间)查询带分页的方法
    */
    /**
    *@Description:模糊查询不成功
    */
    @ApiOperation(value = "条件分页查询讲师")
    //@GetMapping("pageTeacherCondition/{current}/{limit}")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    //只能是post提交   get提交会不成功
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                     @RequestBody
                                              TeacherQuery teacherQuery){
        // 创建page对象
        //调用方法实现分页
        // 调用方法时候  底层封装  把分页所有数据封装到PageTeacher对象里面
        Page<Teacher> teacherPage = new Page<>(current,limit);
        // 构建条件  querywrapper 条件构造方法
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过  动态SQL
        //判断条件值是否为空  如果不为空拼接条件
        String name =teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
//        if (!StringUtils.isEmpty(level) ) {
//            queryWrapper.eq("level", level);
//        }
        queryWrapper.eq("level", level);
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }


        //调用方法实现条件查询分页
        teacherService.page(teacherPage,queryWrapper);

        long total = teacherPage.getTotal();  //得到总记录数
        List<Teacher> pageTeacherRecords = teacherPage.getRecords();  //数据list集合
        return R.ok().data("total",total).data("rows",pageTeacherRecords);
    }
    /**
    *@Description:添加讲师的接口方法
    */
    @PostMapping("addTeacher")
    @ApiOperation(value = "添加讲师")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean teacherFlag = teacherService.save(teacher);
        if (teacherFlag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
    *@Description:根据Id查询讲师
    */
    @GetMapping("getTeacher/{id}")
    @ApiOperation(value = "根据Id查询讲师")
    public R getTeacherById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
       // int i = 10/0;
        return R.ok().data("teacher",teacher);
    }
    /**
    *@Description:讲师修改功能
    */
    @PostMapping("updateTeacher")
    @ApiOperation(value = "根据Id修改讲师")
    public R updateTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return R.ok();
        }else {
            return  R.error();
        }
    }

}

